﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Controls;
using System.Windows.Data;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.DataType;
using SLS.ExClassLib.Data;
namespace SLS.Base
{
    public partial class ExDATA
    {
        #region===============BaseFunction==============
        public override void CMDEXE_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                DataRow row = null;
                ExDataRow dwThis = null;
                e.Source = dwThis;
                if (vwBaseTable.View != null)
                {
                    if ((DataRowView)vwBaseTable.View.CurrentItem != null)
                    {
                        row = ((DataRowView)vwBaseTable.View.CurrentItem).Row;
                        ExDataRow exd = new ExDataRow();
                        dwThis = exd.GetExDataRow(row, true, false, true);
                        e.Source = dwThis;
                    }
                }
                base.CMDEXE_TextChanged(sender, e);
                TextBox cmd = sender as TextBox;
                string cmdtext = cmd.Text;
                switch (cmdtext)
                {
                    case "I":
                        commandInformation(dwThis);
                        break;
                    case "N":
                        commandNew(KSM.Text, KKEYDEFTYPE.Text, KGTPRE.Text, KGTKEY.Text, KKEYINSTANCETABLE.Text);
                        break;
                    case "M":
                        commandModify(dwThis);
                        break;
                    case "C":
                        commandCopy(dwThis);
                        break;
                    case "D":
                        commandDelete(dwThis);
                        break;
                    case "S":
                        commandSave(dwThis);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public override ExDataRow commandNew(string SM, string KEYDEFTYPE, string GTPRE, string GTKEY, string KEYINSTANCETABLE)
        {
            ExDataRow row = null;
            try
            {
                row = base.commandNew(SM, KEYDEFTYPE, GTPRE, GTKEY, KEYINSTANCETABLE);
                DataSet setEvent = new DataSet();
                setEvent = (DataSet)DataAccessService.Instance.ImportDataTable(setEvent, row.Item);
                SetUpTableBase(setEvent, false);
            }
            catch (Exception ex)
            {
                throw;
            }
            return row;
        }
        public override ExDataRow commandCopy(ExDataRow dwThis)
        {
            ExDataRow row = null;
            try
            {
                row = base.commandCopy(dwThis);
            }
            catch (Exception ex)
            {
                throw;
            }
            return row;
        }
        public override void commandDelete(ExDataRow dwThis)
        {
            try
            {
                base.commandDelete(dwThis);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override void commandModify(ExDataRow dwThis)
        {
            try
            {
                base.commandModify(dwThis);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override void commandInformation(ExDataRow dwThis)
        {
            try
            {
                base.commandInformation(dwThis);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public override void commandSave(ExDataRow dwThis)
        {
            try
            {
                dwThis.OnSaveRecord(1);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
        #endregion
        #region===============ExDataRow=================
        /*
        public virtual void LoadLifeData(ExDataRow dwThis, int level)
        {
            dwThis.LoadLifeData(3);
        }
        public void BuildEnvDataSet(ExDataRow dwThis)
        {
            try
            {
                SetEnv.Clear();
                //Here, will load the related table, but will not force to create any data. it willl leav to WorkFlow
                dwThis.LoadLifeData(4);
                SetEnv.Merge(dwThis.Table);
                SetEnv.Merge(ExRowBase.Set4Reference);
                SetEnv.Merge(dwThis.Set2Create);
                SetEnv.Merge(dwThis.Set5KeyDef);
                SetEnv.Merge(dwThis.Set5Property);
                vwGoodsMasterKeyRelation.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                throw;
            }
        }*/
        
        public virtual void SetUpTableBase(DataSet setEvent, bool CheckDataQuality)
        {
            try
            {
                ViewDataSet.SetUpTableBase(setEvent, CheckDataQuality);
                vwBaseTable.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion
        #region===============Tools============
        public virtual void MainGridData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataRow dwThis = null;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                }
                if (drv == null)
                    return;
                dwThis = drv.Row;
                mGDKEY.IsEnabled = false;
                sGDKEY.IsEnabled = false;
                ExDataRow exd = new ExDataRow();
                ActiveDataRow = exd.GetExDataRow(dwThis, false, ViewDataSet.IsCheckDataQuality);
                if (ActiveDataRow == null)
                    return;
                if (dwGoodsMasterKeyRelation == null)
                    return;
                string GDKEY = dwGoodsMasterKeyRelation["GDKEY"].ToString();
                if (GDKEY == "1@2=1")
                {
                    mGDKEY.IsEnabled = true;
                    sGDKEY.IsEnabled = true;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion
    }
}
